package com.dearbinge.opentapi.content.position;

import com.dearbinge.data.api.position.PositionService;
import com.dearbinge.data.pojo.position.Position;
import com.dearbinge.utils.ResultMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Api(description = "坐标接口")
@RestController
@RequestMapping("/position")
@EnableAutoConfiguration
public class PositionController extends WebMvcConfigurerAdapter {

    @Autowired
    private PositionService positionService;

    /**
     * 新增/修改
     */
    @ApiOperation(value = "新增/修改",notes = "新增/修改")
    @RequestMapping(value = "addOrUpdate",method = RequestMethod.POST)
    public ResultMsg<String> addOrUpdate(@RequestBody Position position){
        try {
            if (position.getId() != null && position.getId() != 0){
                Integer dataCode = positionService.update(position);
                if (dataCode > 0){
                    return ResultMsg.ok("修改成功");
                }
            }else {
                Integer dataCode = positionService.insert(position);
                if (dataCode > 0){
                    return ResultMsg.ok("新增成功");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }


    /**
     * 根据ID或用户DI删除
     */
    @ApiOperation(value = "根据ID删除",notes = "根据ID删除")
    @ApiImplicitParams({@ApiImplicitParam(name = "id",value = "id",paramType = "query"),
            @ApiImplicitParam(name = "userId",value = "用户ID",paramType = "query")})
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public ResultMsg<String> delete(Integer id,String userId){
        try {
            Integer dataCode = positionService.delete(id,userId);
            if (dataCode > 0){
                return ResultMsg.ok("删除成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     *根据用户ID查询
     */
    @ApiOperation(value = "根据用户ID查询",notes = "根据用户ID查询")
    @ApiImplicitParam(name = "userId",value = "用户ID",paramType = "query")
    @RequestMapping(value = "/getByUserId",method = RequestMethod.GET)
    public ResultMsg<Position> getByUserId(String userId){
        try {
            Position position = positionService.getByUserId(userId);
            return ResultMsg.ok(position);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

}
